package _15_generics._15_3.exercise;

import _15_generics._15_3.Fibonacci;

import java.util.Iterator;

/**
 * Use composition instead of inheritance to adapt
 * Fibonacci to make it Iterable
 */
public class E07_IterableFibonacci2 {
    public static void main(String[] args) {
        for (int n : new IterableFibonacci(18)) {
            System.out.print(n +" ");
        }
    }
}

class IterableFibonacci implements Iterable<Integer>{

    private Fibonacci fibonacci ;
    private int n ;
    public IterableFibonacci(int n){
        fibonacci = new Fibonacci();
        this.n = n;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            @Override
            public boolean hasNext() {
                return n > 0;
            }

            @Override
            public Integer next() {
                n--;
                return fibonacci.next();
            }
        };
    }
}
